﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Device
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Image1.ImageUrl = "../WebForm1.aspx";
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            TextBox1.Text = null;
            TextBox2.Text = null;
            TextBox3.Text = null;
            TextBox4.Text = null;
            TextBox5.Text = null;
            TextBox6.Text = null;
            TextBox7.Text = null;
            TextBox8.Text = null;
            TextBox9.Text = null;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string CodeError = "校验码错误！请重新输入！";
            string GetCode = "请获取验证码！";
            var checkCode = TextBox9.Text.Trim();
            RegionCode.Result result = RegionCode.CheckCodeSame(checkCode);
            
                switch (result)
                {
                    case RegionCode.Result.Fail:
                        Util.AlertString(CodeError, Response);
                        break;
                    case RegionCode.Result.Success:
                        CheckSerialNum();
                        break;
                    case RegionCode.Result.CodeNull:
                        Util.AlertString(GetCode, Response);
                        break;
                }
        }

        //判断序列号
        void CheckSerialNum()
        {
            string Success = "请到您注册的邮箱激活账号!";
            string NumUsed = "抱歉,该序列号已被注册!";
            string Inexistence = "抱歉,您填写的序列号不存在!";
            string UserUsed = "抱歉,该邮箱已被注册!";          
            var username = TextBox2.Text.Trim();
            var serialnum = TextBox1.Text.Trim();
            var password = TextBox3.Text.Trim();
            var email = TextBox5.Text.Trim();
            var name = TextBox6.Text.Trim();
            var answer = TextBox7.Text.Trim();
            var note = TextBox8.Text.Trim();
            var question = DropDownList1.Text.Trim();
            Session["usernames"] = username;
            Session["times"] = DateTime.Now.AddMinutes(5).ToString();
           
           
            var time = (string)Session["times"];
            EquipmentMaster sh = new EquipmentMaster();                  
            DeviceMaster.AuthResult addResult = DeviceMaster.add(serialnum, email);
            switch (addResult)
            {               
                case DeviceMaster.AuthResult.SerialNumnotNull:
                    Util.AlertString(NumUsed, Response);
                    break;
                case DeviceMaster.AuthResult.Register:
                    DeviceMaster.UserRegister(username, password, email, name, answer, note, question);
                    DeviceMaster.DeviceRegister(serialnum, email);
                    var userid = DeviceMaster.CheckUserId(email);
                    Session["userids"] = DeviceMaster.CheckUserId(email);
                    Util.registerSendEmail(username, email, time, userid);
                    Util.AlertString(Success, Response);
                    break;
                case DeviceMaster.AuthResult.NumDoesnotMatch:
                    Util.AlertString(Inexistence, Response);
                    break;
                case DeviceMaster.AuthResult.EmailExist:
                    Util.AlertString(UserUsed, Response);
                    break;
            }
        }

   
        protected void Button3_Click(object sender, EventArgs e)
        {
            Image1.ImageUrl = "../WebForm1.aspx";
        }
    }
}